﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Arizu.DAL;
using Arizu.Entity;

namespace Arizu.Repository
{
    public class AirizuMessage : BaseProvider
    {
        #region
        private static volatile AirizuMessage instance;
        private static object lockHelper = new object();

        private AirizuMessage()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static AirizuMessage CreateInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new AirizuMessage();
                    }
                }
            }
            return instance;
        }
        #endregion

        /// <summary>
        /// 插入Message信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public long InsertMessage(MessageEntity message)
        {
            string sql = "INSERT INTO Message (Message,Sender,Reciver,State,Type) VALUES (@Message,@Sender,@Reciver,@State,@Type)";

            #region parameters
            SqlParameter parm_Message = new SqlParameter("@Message", message.Message);
            SqlParameter parm_Sender = new SqlParameter("@Sender", message.Sender);
            SqlParameter parm_Reciver = new SqlParameter("@Reciver", message.Reciver);
            SqlParameter parm_State = new SqlParameter("@State", message.State);
            SqlParameter parm_Type = new SqlParameter("@Type", message.Type);

            SqlParameter[] parameters = { parm_Message, parm_Sender, parm_Reciver, parm_State, parm_Type };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);

                //message.ID = long.Parse(ds.Tables[0].Rows[0][0].ToString());

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            return message.ID;
        }

        /// <summary>
        /// 获取消息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetMesage(int userId)
        { 
            DataTable dt = new DataTable();
            string sql = "select message from message where reciver=" + userId;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        /// <summary>
        /// 获取消息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetSysMesage(int userId)
        {
            DataTable dt = new DataTable();
            string sql = "select message from message where reciver=" + userId + " and Type=" + (int)MessageType.System;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }


        /// <summary>
        /// 获取消息数量
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetMesageCount(int userId)
        {
            DataTable dt = new DataTable();
            string sql = "select count(*) from message where reciver=" + userId;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        /// <summary>
        /// 获取未读消息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetUnreadMesage(int userId)
        {
            DataTable dt = new DataTable();
            string sql = "select message from message where reciver=" + userId + " and state=0";

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        /// <summary>
        /// 获取未读消息数量
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetUnreadMesageCount(int userId)
        {
            DataTable dt = new DataTable();
            string sql = "select count(*) from message where reciver=" + userId + " and state=0";

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }
    }

    /// <summary>
    /// 消息类型
    /// </summary>
    public enum MessageType : int
    {
        System = 0, 
        User = 1
    }

    /// <summary>
    /// 消息状态
    /// </summary>
    public enum MessageState : int
    {
        Read = 1, 
        Unread = 0
    }
}
